package com.meterreadingsys.util;



import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXUtil extends DefaultHandler{
	
	public String key;
	
	public String value;
	
	public String preTag="";//标识
	
	public boolean b=false;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public SAXUtil(String key){
		
		this.key=key;
		
	}
	
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(preTag.length()>0){
			String data=new String(ch,start,length);
			if("port".equals(preTag)){
				this.value=data;
			
				endDocument();
			}else if("ip".equals(preTag)){
				this.value=data;
				System.out.println(data);
				endDocument();
				
			}
		}
		
	}

	

	@Override
	public void endDocument() throws SAXException {
		
		super.endDocument();
		System.out.println("结束读取");
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		preTag="";
		super.endElement(uri, localName, qName);
	}

	@Override
	public void startDocument() throws SAXException {
		System.out.println("开始读取XML");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		System.out.println("开始读取元素");
		if(key.equals(localName)){
			this.preTag=localName;
		}
		
	}
   
	
}
